/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ch.repit.aglaja.server.contacts;

import ch.repit.aglaja.client.contacts.AddressDef;
import ch.repit.aglaja.client.contacts.PersonDef;
import ch.repit.aglaja.client.contacts.TagDef;
import ch.repit.rwt.client.Bento;
import ch.repit.rwt.client.BentoDef;
import ch.repit.rwt.client.BentoDefFactory;
import ch.repit.rwt.client.ObjectRef;
import ch.repit.rwt.client.persistence.ObjectNotFoundException;
import ch.repit.rwt.client.util.CountryCodes;
import ch.repit.rwt.server.persistence.JdoHelper;
import ch.repit.rwt.server.user.UserAddressStickersPdf;
import ch.repit.rwt.server.util.Logging;
import com.itextpdf.text.Phrase;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *
 * @author tc149752
 */
public class PersonAddressStickersPdf extends UserAddressStickersPdf {

    private static Logging LOG = new Logging(PersonAddressStickersPdf.class.getName());

    private Bento restrictedPerson = null;

    @Override
    protected List<Bento> filterBentos() {
        String meth="filterBentos";
        LOG.enter(meth, "restrict2bento:"+restrict2bento);
        List<Bento> res = null;

        if (restrict2bento != null) {
            // is it a Tag?
            if (TagDef.TYPE.equals(restrict2bento.getType())) {
                
                try {
                    // get the address refs of the tag
                    Bento tag = JdoHelper.get().getBento(restrict2bento);
                    List<String> addressList = tag.get(TagDef.ATTR_ADDRESSES).getStringList();

                    // get all the addresses
                    BentoDef addressDef = BentoDefFactory.get().getDef(AddressDef.TYPE);
                    Map<String,String> qp = new HashMap();
                    qp.put("ordering", " country asc, locality asc");
                    List<Bento> allAddresses = JdoHelper.get().listActiveBentos(addressDef, qp);

                    // keep the ones that match
                    res = new ArrayList();
                    for (Bento a : allAddresses) {
                        if (addressList.contains(a.getRef().toString()))
                            res.add(a);
                    }
                } catch (ObjectNotFoundException e) {
                    LOG.error(meth, "Tag not found:"+restrict2bento.toString(), e);
                }
            }
            
            // is it an Address?
            else if (AddressDef.TYPE.equals(restrict2bento.getType())) {
                try {
                    Bento address = JdoHelper.get().getBento(restrict2bento);
                    res = new ArrayList();
                    res.add(address);
                } catch (ObjectNotFoundException e) {
                    LOG.error(meth, "Address not found:"+restrict2bento.toString(), e);
                }
            }
            
            // is it a Person
            else if (PersonDef.TYPE.equals(restrict2bento.getType())) {
                try {
                    restrictedPerson = JdoHelper.get().getBento(restrict2bento);
                    ObjectRef aref = new ObjectRef(restrictedPerson.getDisplayValue(PersonDef.ATTR_ADDRESSREF));
                    Bento address = JdoHelper.get().getBento(aref);
                    res = new ArrayList();
                    res.add(address);

                } catch (ObjectNotFoundException e) {
                    LOG.error(meth, "Address not found:"+restrict2bento.toString(), e);
                }
            }

        }

        // otherwise, we take everyone
        else {
            BentoDef addressDef = BentoDefFactory.get().getDef(AddressDef.TYPE);
            Map<String,String> qp = new HashMap();
            qp.put("ordering", " country asc, locality asc");
            res = JdoHelper.get().listActiveBentos(addressDef, qp);
        }

        LOG.leave(meth, "result size:"+res==null?"null":(""+res.size()));
        return res;
    }

    
    @Override
    protected Phrase formatSticker(Bento addressBento) {
        Phrase address = new Phrase();
        AddressDef addressDef = (AddressDef)BentoDefFactory.get().getDef(AddressDef.TYPE);

        // if the sticker is for a specific person
        if (restrictedPerson != null) {
            List<Bento> singlePerson = new ArrayList();
            singlePerson.add(restrictedPerson);
            addressBento.get(AddressDef.ATTR_DISPLAYNAMES).set(Boolean.TRUE);
            String formatedNames = addressDef.formatNames(addressBento, singlePerson);
            address.add(new Phrase(formatedNames+"\n",font));
        }

        else {
            if (addressBento.get(AddressDef.ATTR_HEADER) != null
                    && addressBento.get(AddressDef.ATTR_HEADER).getString() != null
                    && addressBento.get(AddressDef.ATTR_HEADER).getString().trim().length() > 0 )
                address.add(new Phrase(addressBento.get(AddressDef.ATTR_HEADER).getString() + "\n", font));

            // if needed, retrieves the persons for this address
            if (addressBento.get(AddressDef.ATTR_DISPLAYNAMES).getBoolean() != null) {
                BentoDef personDef = BentoDefFactory.get().getDef(PersonDef.TYPE);
                Map<String,String> qp = new HashMap();
                qp.put("ordering", " lastName asc nulls last, maleGender asc");
                qp.put("filter", " " + PersonDef.ATTR_ADDRESSREF + " == '" + addressBento.getRef().toString()+"'");
                List<Bento> personList = JdoHelper.get().listActiveBentos(personDef, qp);

                // TBD: here, if restrict2bento is a Person, only retain this person + force fullname (not supported yet)

                String formatedNames = addressDef.formatNames(addressBento, personList);
                address.add(new Phrase(formatedNames+"\n",font));
            }
        }

        if (addressBento.get(AddressDef.ATTR_ADDRESSLINE) != null && addressBento.get(AddressDef.ATTR_ADDRESSLINE).getString() != null )
            address.add(new Phrase(addressBento.get(AddressDef.ATTR_ADDRESSLINE).getString() + "\n", font));
        
        if (addressBento.get(AddressDef.ATTR_ZIP) != null && addressBento.get(AddressDef.ATTR_ZIP).getString() != null )
            address.add(new Phrase(addressBento.get(AddressDef.ATTR_ZIP).getString() + " ", font));
        if (addressBento.get(AddressDef.ATTR_LOCALITY) != null && addressBento.get(AddressDef.ATTR_LOCALITY).getString() != null )
            address.add(new Phrase(addressBento.get(AddressDef.ATTR_LOCALITY).getString(), fontBold));
        
        if (addressBento.get(AddressDef.ATTR_COUNTRY) != null && addressBento.get(AddressDef.ATTR_COUNTRY).getString() != null )
            if (!CountryCodes.CH.equals(addressBento.get(AddressDef.ATTR_COUNTRY).getString()))
                address.add(new Phrase("\n" + CountryCodes.getCountryCodeMap().get(addressBento.get(AddressDef.ATTR_COUNTRY).getString()), font));
        
        return address;
    }
    
}
